<?php
class EventController extends AppController
{
    public $name = 'Event';
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    public $uses = array(
        'tbl_event','tbl_log'
    );

    public function event_setting()
    {
        /*foreach($this->data as $key=>$value)
        {
            if ( is_array($value))
            {
                echo $key. '<br />' ;
                foreach($value as $key2=>$value2)
                    echo $key2."=>".$value2. '<br />';
            }
            else
                echo $key."=>".$value. '<br />';

            echo '-----<br />' ;
        }*/

        $event_list = array();
        $event_reward_list = array();
        $event_reward_cnt_list  = array();
        $event_message_list = array();

        $event_list_db = $this->tbl_event->loadEventData();
        $event_value = '' ;
        foreach($event_list_db as $event_value )
        {
            //print_r($event_value);
            //echo('<br />');
            if ($event_value['tbl_event']['event_flag'])
                $event_list[$event_value['tbl_event']['type']] = 1;
            else
                $event_list[$event_value['tbl_event']['type']] = 0;

            //일일 접속 이벤트 아이템 추가
            if ($event_value['tbl_event']['type'] == __(LANG_EVENT_DAILY_CONNECT))
            {
                if ( $event_value['tbl_event']['reward_type'] ==  __(LANG_GOLD))
                    $event_reward_list[$event_value['tbl_event']['type']] = 2;
                else if ( $event_value['tbl_event']['reward_type'] ==  __(LANG_CRYSTAL))
                    $event_reward_list[$event_value['tbl_event']['type']] = 1;
                else if ($event_value['tbl_event']['reward_type'] ==  __(LANG_FRIEND))
                    $event_reward_list[$event_value['tbl_event']['type']] = 3;
                else
                    $event_reward_list[$event_value['tbl_event']['type']] = 4;

                $event_reward_cnt_list[$event_value['tbl_event']['type']] = $event_value['tbl_event']['count'] ;
                $event_message_list[$event_value['tbl_event']['type']] = $event_value['tbl_event']['message'] ;
            }
        }

        //print_r($event_list);
        //echo('<br />');

        $this->set(compact('event_list','event_reward_list','event_reward_cnt_list','event_message_list'));
    }

    public function set_event()
    {
        $evnet_info = $this->data['evnet_info'];
        $evnet_onoff =  $evnet_info['on_off'];
        $event_url = '';
        $evnet_type = $evnet_info['type'] ;
        $data = '' ;

        if ($evnet_type == __(LANG_EVENT_DAILY_CONNECT))
        {
            $item_type_cnt = $evnet_info['item_type'] ;
            $item_cnt = $evnet_info['item_cnt'] ;
            $item_type = '';

            if ($item_type_cnt == 1 && is_numeric($item_cnt))
            {
                $item_type = 'Crystal';
            }
            else if ($item_type_cnt == 2 && is_numeric($item_cnt))
            {
                $item_type = 'Gold';
            }
            else if ($item_type_cnt == 3 && is_numeric($item_cnt))
            {
                $item_type = 'Friend';
            }
            else
            {
                $item_type = 'Stamina';
            }

            $event_url = $this->server_url . '/daily_connect_event';
            $data = array('reward_id'=>$item_type,'reward_type'=>$item_type,'message'=>__(LANG_EVENT_DAILY_CONNECT_MESSAGE),'count'=>$item_cnt,'event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');
            $event_list[__(LANG_EVENT_DAILY_CONNECT)] = $data['event_flag'] ;
        }
        else if ($evnet_type == __(LANG_EVENT_FIRST_BILLING))
        {
            $event_url = $this->server_url . '/first_billing_event';
            //echo $evnet_onoff ;
            //echo('<br />');
            $data = array('event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');//true 면 false 로 설정해야한다
            $event_list[__(LANG_EVENT_FIRST_BILLING)] = $data['event_flag'] ;
        }
        else if ($evnet_type == __(LANG_EVENT_FARM_HARVEST))
        {
            $event_url = $this->server_url . '/farm_harvest_event';
            $data = array('event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');//true 면 false 로 설정해야한다
            $event_list[__(LANG_EVENT_FARM_HARVEST)] = $data['event_flag'] ;
        }
        else if ($evnet_type == __(LANG_EVENT_LEVEL_UP))
        {
            $event_url = $this->server_url . '/level_up_event';
            $data = array('event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');//true 면 false 로 설정해야한다
            $event_list[__(LANG_EVENT_LEVEL_UP)] = $data['event_flag'] ;
        }
        else if ($evnet_type == __(LANG_EVENT_STAGE_CLEAR))
        {
            $event_url = $this->server_url . '/stage_clear_event';
            $data = array('event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');//true 면 false 로 설정해야한다
            $event_list[__(LANG_EVENT_STAGE_CLEAR)] = $data['event_flag'] ;
        }
        else
        {
            echo('--------------Event Type 없음!!!!!!!!!!!!-----------------');
        }

        //print_r($data);
        $result_data = $data ;

        App::uses('HttpSocket', 'Network/Http');
        $HttpSocket = new HttpSocket();
        $result = json_decode($HttpSocket->post($event_url, $data), true);

        $result_data = $data ;

        $result_code = $result['rcode'] ;
        $result_msg = $result['message'] ;
        if($result['rcode'] == 0) {
            $result_info = '이벤트 설정 성공' ;
        } else {
            $result_info = '이벤트 설정 실패.' ;
        }

        //로그 설정
        $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$event_url,'operation_data'=>$data,'operation_result'=>$result_info);
        $this->tbl_log->save($logdata);

        $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
    }

    public function set_event_item()
    {
        $evnet_item_info = $this->data['evnet_item_info'];
        $evnet_onoff =  $evnet_item_info ['on_off'];
        $event_url = '';
        $item_type_cnt = $evnet_item_info ['item_type'] ;
        $item_type = '' ;
        $item_cnt =  $evnet_item_info ['evnet_item_cnt'];
        $evnet_message = $evnet_item_info ['evnet_message'];

        $result_info = '';
        $result_code = '';
        $result_msg = '' ;
        $result_info = '';

        $data = '';

        if (empty($evnet_message))
        {
            $result_info = '적용 메시지 입력하세요!!!';
            $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
            return ;
        }

        if ($item_type_cnt == 1 && is_numeric($item_cnt))
        {
            $item_type = 'Crystal';
        }
        else if ($item_type_cnt == 2 && is_numeric($item_cnt))
        {
            $item_type = 'Gold';
        }
        else if ($item_type_cnt == 3 && is_numeric($item_cnt))
        {
            $item_type = 'Friend';
        }
        else if ($item_type_cnt == 4 && is_numeric($item_cnt) )
        {
            $item_type = 'Stamina';
        }
        else
        {
            $result_info = '--------------아이템 CNT 숫자로 입력하세요!!!!!!!!!!!!-----------------';
            $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
            return ;
        }

        $data = array('reward_id'=>$item_type,'reward_type'=>$item_type,'message'=>$evnet_message,'count'=>$item_cnt,'event_flag'=>$evnet_onoff,'start_time'=>'','end_time'=>'');
        //print_r($data);
        $result_data = $data ;

        $event_url = $this->server_url. '/daily_connect_event';
        App::uses('HttpSocket', 'Network/Http');
        $HttpSocket = new HttpSocket();
        $result = json_decode($HttpSocket->post($event_url, $data), true);

        $result_data = $data ;

        $result_code = $result['rcode'] ;
        @$result_msg = $result['message'] ;
        if($result['rcode'] == 0) {
            $result_info = '이벤트 아이템 적용 성공' ;
        } else {
            $result_info = '이벤트 아이템 적용 실패.' ;
        }

        //로그 설정
        $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>$event_url,'operation_data'=>$data,'operation_result'=>$result_info);
        $this->tbl_log->save($logdata);

        $this->set(compact('result_code', 'result_msg','result_info', 'result_data'));
    }
}